home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5028 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.6 KB  |  61 lines

  1. Newsgroups: comp.lang.c++
  2. Path: howland.reston.ans.net!torn!nott!emr1!jagrant
  3. From: jagrant@emr1.emr.ca (John Grant)
  4. Subject: simple design question
  5. Message-ID: <DM4u8I.G1H@emr1.emr.ca>
  6. Organization: Energy, Mines, and Resources, Ottawa
  7. Date: Fri, 2 Feb 1996 05:07:30 GMT
  8.  
  9. This is a design question, not a syntax question, so forgive the pseudo-
  10. code.
  11.  
  12. I'm trying to design/write a class for complex file I/O (I'll state it
  13. in simple terms here).
  14.  
  15. I want to use it like this:
  16.     object.Open("xxx")
  17.     for(...){
  18.       object.Read(stuff);
  19.     }
  20.     object.Close();
  21.  
  22. But I want to also have a single function that will Open/Read/Close in a
  23. single operation:
  24.     object.GetData("xxx",stuff);
  25.  
  26. The simple question is: should I implement GetData() as part of this
  27. class, i.e.:
  28.     int myclass::GetData(...){
  29.     {    Open()
  30.         Read()
  31.         Close()
  32.     }
  33.  
  34. or should GetData() really be in a separate class which uses the first class:
  35.     int myclass2::GetData(...)
  36.     { myclass *object=new myclass;
  37.         object->Open(...)
  38.         object->Read(...)
  39.         object->Close(...);
  40.         delete object;
  41.     }
  42.  
  43. If so, is that the best way to do it, or is there a slicker C++ way to do
  44. that using inheritance etc (which I am just trying to figure out).
  45.  
  46. The reason I ask, is the potential for conflict in the use of the
  47. private variables. For example, internally there might be:
  48.     private:    FILE *f;
  49.             <...more internals...>
  50.  
  51. If I used it incorrectly, i.e.:
  52.     object.Open(...)
  53.     object.GetData(...)    //does Open/Read/Close
  54.     object.Read(...)
  55. then object.Read() would break because GetData() calls Close(), invalidating
  56. 'f' and other internals.
  57. -- 
  58. John A. Grant                        jagrant@emr1.emr.ca
  59. Airborne Geophysics
  60. Geological Survey of Canada, Ottawa
  61.